package com.ms.game.sendgift.db;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;

import com.ms.game.itembox.message.SSimpleItem;
import com.ms.game.sendgift.message.SendGiftResponse;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class SaveGiftRecordListLogAsync extends TaskAsyncCommand {

	public ArrayList<SendGiftNotifyModel> timeOutData;
	
	public String playerIP;
	
	public GUID sessionID;
	
	public SendGiftResponse response;
	
	/**
	 * 最終金錢數量
	 */
	public BigDecimal coin;
	
	/**
	 * 最終道具數量
	 */
	public HashMap<String , SSimpleItem> items = new HashMap<String , SSimpleItem>();
	
	@Override
	public void asyncExecute() throws Exception {
		
		ArrayList<SendGiftRecordLog> logList = new ArrayList<SendGiftRecordLog>();
		for(SendGiftNotifyModel model :timeOutData)
		{
			SendGiftRecordLog logModel = new SendGiftRecordLog();
			logModel.setOrderID(model.getOrderID());
			logModel.setSendOrderID(model.getSendOrderID());
			logModel.setOrderNumber(model.getOrderNumber());
			logModel.setPlayerGUID(model.getPlayerGUID());
			logModel.setPlayerName(model.getPlayerName());
			logModel.setTargetGUID(model.getTargetGUID());
			logModel.setTargetName(model.getTargetName());
			logModel.setGoldCoin(model.getGoldCoin());
			logModel.setServiceCharge(model.getServiceCharge());
			logModel.setItems(model.getItems());
			logModel.setStage(model.getStage());
			logModel.setCreateTime(model.getCreateTime());
			logModel.setAnswerTime(model.getAnswerTime());
			//過期時間
			long completeTime = TimeSys.currentTimeMills();
			Date completeData = new Date(completeTime);
			logModel.setCompleteTime(completeData);
			logModel.setOutTakenTime(completeData);
			logModel.setSuccess(false);
			logModel.setFinish(true);
			logModel.setTaken(true);
			logModel.setPlayerIP(playerIP);
			logList.add(logModel); //不是自己的log要先寫
			
			//拿取寫標記
			model.setTaken(true);
		}
		
		if(logList.size() > 0){
			SendGiftRecordDAO.saveOrUpdateListSendGiftLog(logList);
			SendGiftDAO.saveOrUpdateListSendGiftNotify(timeOutData);
		}
		
	}

	@Override
	public void execute() throws Exception {
		
		response.nowGoldCoin = coin.toString();
		response.items = items;
		response.errorCode = ErrorCode.SUCCESS;
		MessageAction.sendMessage(sessionID, MessageTypes.S_GIFT_CHECK_TIME_OUT_RESPONSE, response);

	}

}
